package unit04.day05;
/**
 * 互斥锁
 * synchronized也叫互斥锁，即：
 * 使用synchronized修饰多段代码，只要他们的同步监视器对象相同
 * 那么这几段代码间就是互斥关系，即：多个线程不能同时执行这些代码
 *
 */
public class SyncDemo4 {

	public static void main(String[] args) {
		final Boo b1 = new Boo();
		Thread t1 = new Thread() {
			public void run() {
				b1.methodA();
			}
		};
		Thread t2 = new Thread() {
			public void run() {
				b1.methodA();
			}
		};
		t1.start();
		t2.start();
	}

}
class Boo{
	public synchronized void methodA() {
		try {
			Thread t = Thread.currentThread();
			System.out.println(t+":方法A正在运行");
			Thread.sleep(3000);
			System.out.println(t+":方法A运行完毕");
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	public synchronized void methodB() {
		try {
			Thread t = Thread.currentThread();
			System.out.println(t+":方法B正在运行");
			Thread.sleep(3000);
			System.out.println(t+":方法B运行完毕");
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}